<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../axios/axios.min.js"></script>
</head>

<body>
    <div>
        <h1>获取省份</h1>
        省:<input type="text" class="province">
        市:<input type="text" class="city">

        <button class='resolve'>成功</button>
        <p></p>
    </div>
    <script>
        document.querySelector('.resolve').addEventListener('click', function () {
            const pname = document.querySelector('.province').value
            const cname = document.querySelector('.city').value
            console.log(pname);
            console.log(cname);
            
            function axios(config) {
                return new Promise((resolve, reject) => {
                    if (config.params) {
                        const usp = new URLSearchParams(config.params)
                        const p = usp.toString()
                        config.url += `?${p}`
                        console.log(p);
                        
                        console.log(config.url);

                    }
                    const xhr = new XMLHttpRequest()
                    xhr.open(config.method || 'GET', config.url)
                    xhr.addEventListener('loadend', function () {
                        if (xhr.status >= 200 && xhr.status <= 299) {
                            resolve(JSON.parse(xhr.response))
                        } else {
                            reject(new Error(xhr.response))
                        }

                    })
                    xhr.send()
                })
            }
            axios({
                url: 'http://hmajax.itheima.net/api/are',
                params: {
                    pname,
                    cname
                }
            }).then(res => {
                document.querySelector('p').innerHTML = res.message

            }).catch(err => {
                console.log(err);
                
                document.querySelector('p').innerHTML = err.message

            }
            )
        })
    </script>
</body>

</html>